<?php

eval( '

function '.$mydirname.'_global_search( $keywords , $andor , $limit , $offset , $userid )
{
    return d3downloads_global_search_base( "'.$mydirname.'" , $keywords , $andor , $limit , $offset , $userid ) ;
}

' ) ;

if( ! function_exists( 'd3downloads_global_search_base' ) ) {
	function d3downloads_global_search_base($mydirname, $queryarray, $andor, $limit, $offset, $userid)
	{
		$db =& Database::getInstance() ;

		$showcontext = isset( $_GET['showcontext'] ) ? $_GET['showcontext'] : 0 ;
		require_once dirname(__FILE__).'/include/common_functions.php' ;

		$whr_cat4read = "cid IN (".implode(",",d3download_can_read( $mydirname ) ).")" ;
		if( $showcontext == 1){
			$sql = "SELECT lid, cid, title, description, submitter, date FROM ".$db->prefix( $mydirname."_downloads" )." WHERE visible = '1' AND ($whr_cat4read)";
		} else {
			$sql = "SELECT lid, cid, title, submitter, date, title FROM ".$db->prefix( $mydirname."_downloads" )." WHERE visible = '1' AND ($whr_cat4read)";
		}

		if ( $userid != 0 ) {
			$sql .= " AND submitter=".$userid." ";
		}

		// because count() returns 1 even if a supplied variable
		// is not an array, we must check if $querryarray is really an array
		if ( is_array( $queryarray ) && $count = count( $queryarray ) ) {
			$sql .= " AND ( ( title LIKE '%$queryarray[0]%' OR description LIKE '%$queryarray[0]%' )";
			for($i=1;$i<$count;$i++){
				$sql .= " $andor ";
				$sql .= "( title LIKE '%$queryarray[$i]%' OR description LIKE '%$queryarray[$i]%' )";
			}
			$sql .= ") ";
		}
		$sql .= "ORDER BY date DESC";

		$myts =& MyTextSanitizer::getInstance();
		$result = $db->query($sql, $limit, $offset);
		$ret = array();

		while($myrow = $db->fetchArray($result) )
		{
			$lid = intval( $myrow['lid'] );
			$cid = intval( $myrow['cid'] );
			$title4assign = $myts->makeTboxData4Show( $myrow['title'] )  ;
			$date4assign = intval( $myrow['date'] );
			$submitter4assign = intval( $myrow['submitter'] );
			$body = $myts->displayTarea( $myrow['description'], 0, 1, 1, 1, 1) ;
			if ( strstr ( $body , '[pagebreak]' ) ){
				$desc_string = explode( '[pagebreak]', $body , 2 ) ;
				$body4assign = $desc_string[0];
			} else {
				$body4assign = $body ;
			}

			$context = '' ;
			// get context for module "search"
			if( function_exists( 'search_make_context' ) && $showcontext ) {
				if( function_exists( 'easiestml' ) ) $text = easiestml( $body4assign ) ;
				$full_context = strip_tags( $body4assign ) ;
				$context = search_make_context( $full_context , $keywords ) ;
			}

			$ret[] = array(
				'link' => "index.php?page=singlefile&amp;cid=$cid&amp;lid=$lid" ,
				'title' => $title4assign ,
				'time' => $date4assign ,
				'uid' => $submitter4assign ,
				'context' => $context ,
			) ;
		}
		return $ret;
	}
}

?>